const fs = require("fs");//文件读写
const parse = require("@babel/parser"); //解析为ast
const traverse = require('@babel/traverse').default;//遍历节点
const t = require('@babel/types');//类型
const generator = require('@babel/generator').default;//ast解析为代码


//读取js文件
const jscode = fs.readFileSync(
    './a.js', {
        encoding: 'utf-8'
    }
);
let ast = parse.parse(jscode);//js转ast

function remove_comma(path) {
    // 去除逗号表达式
    if (t.isExpressionStatement(path.node)) {
        let {expression} = path.node
        if (!t.isSequenceExpression(expression))
            return;
        let body = []
        expression.expressions.forEach(
            express => {
                body.push(t.expressionStatement(express))
            }
        )
        path.replaceInline(body)
    }
    // var a,b,c;变成var a; var b;var c;
    if (t.isVariableDeclaration(path.node)) {
        let kind = path.node.kind;
        let declarationslist = path.node.declarations;
        let declength = declarationslist.length;
        if (declength < 1) return;
        if(declength == 1&&!t.isAssignmentExpression(declarationslist[0].init))return;
        let body = []
        for (i = 0; i < declength; i++) {
            if (t.isAssignmentExpression(declarationslist[i].init)) {
                let tmp_list = [declarationslist[i].id.name];
                let otherlist = [];
                let endresult = ''
                let whilesign = declarationslist[i].init;
                while (t.isAssignmentExpression(whilesign) && whilesign.operator == '=') {
                    if (t.isIdentifier(whilesign.left)) {
                        tmp_list.push(whilesign.left.name);
                    } else {
                        otherlist.push(generator(whilesign.left).code);
                    }
                    whilesign = whilesign.right
                    endresult = generator(whilesign).code;

                }
                for (j = 0; j < tmp_list.length; j++) {
                    let varDec = t.VariableDeclarator(t.identifier(tmp_list[j]), whilesign);//obj=上面注释
                    let localAST = t.VariableDeclaration(kind, [varDec]);// let obj=上面注释
                    body.push(localAST)
                }
                for (j = 0; j < otherlist.length; j++) {
                    let localAST = t.identifier(otherlist[j]+'='+endresult);//obj=上面注释
                    body.push(localAST)
                }

            } else {
                let varDec = t.VariableDeclarator(t.identifier(generator(declarationslist[i], {compact: true},).code));//obj=上面注释
                let localAST = t.VariableDeclaration(kind, [varDec]);// let obj=上面注释
                body.push(localAST)
            }

        }
        path.replaceInline(body)
        // path.stop()//节点替换后注意是否会陷入死循环，陷入则取消注释
    }


}
traverse(ast, {"ExpressionStatement|VariableDeclaration": remove_comma,});        // 序列表达式还原
let {code} = generator(ast,opts = {jsescOption:{"minimal":true}})
//文件保存
fs.writeFile('./a_dec.js', code, (err) => {
});

